import nprogress from "nprogress"

// 当前活跃的消息实例
let currentMessageInstance = null

// 消息提示 - 确保同时只显示一个消息
export function showMessage(message = '提示内容', type = 'success', customClass = '') {
    // 如果有当前活跃的消息，先关闭它
    if (currentMessageInstance) {
        currentMessageInstance.close()
        currentMessageInstance = null
    }
    
    // 创建新的消息实例
    currentMessageInstance = ElMessage({
        type: type,
        message,
        customClass,
        duration: 1500, // 1.5秒后自动关闭，提供快速反馈
        showClose: false, // 不显示关闭按钮，让消息更简洁
        onClose: () => {
            currentMessageInstance = null // 消息关闭时清除引用
        }
    })
    
    return currentMessageInstance
}

// 弹出确认框
export function showModel(content = '提示内容', type = 'warning', title = '') {
    return ElMessageBox.confirm(
        content,
        title,
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type,
        }
    )
}

// 显示页面加载 Loading
export function showPageLoading() {
    nprogress.start()
}

// 隐藏页面加载 Loading
export function hidePageLoading() {
    nprogress.done()
}